package com.yanfan;

import org.openscada.opc.lib.common.ConnectionInformation;
import org.openscada.opc.lib.da.*;
import org.openscada.opc.lib.da.browser.FlatBrowser;

import java.util.concurrent.Executors;

public class OpcDaClient {
    public static void main(String[] args) {
        // 连接 OPC 服务器的基本信息
        ConnectionInformation ci = new ConnectionInformation();
        ci.setHost("192.168.23.6"); // OPC 服务器 IP
        ci.setDomain("");        // 域（如果有的话）
        ci.setUser("sa");  // 用户名
        ci.setPassword("123456"); // 密码

        // 创建 OPC 服务器连接
        Server server = new Server(ci, Executors.newSingleThreadScheduledExecutor());
        try {
            server.connect();
            System.out.println("连接成功!");

            // 使用 SyncAccess 定时获取数据
            SyncAccess access = new SyncAccess(server, 1000);
            access.addItem("Simulation Examples.Functions.Ramp1", new DataCallback() {
                @Override
                public void changed(Item item, ItemState state) {
                    System.out.println("Ramp1: " + state.getValue());
                }
            });
            access.bind();
            Thread.sleep(5000);
            access.unbind();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            server.disconnect();
        }
    }
}
